//
//  CHSArrayCrashController.m
//  CAIAvoidCrash_Example
//
//  Created by 朱新明 on 2023/9/1.
//  Copyright © 2023 海参. All rights reserved.
//

#import "CHSArrayCrashController.h"

@interface CHSArrayCrashController ()

@property (nonatomic, copy) NSArray *dataArray;


@end

@implementation CHSArrayCrashController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self setUI];
    [self setData];
}
- (void)setUI{
    self.navigationItem.title = @"NSArray/NSMutableArray";
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
}

#pragma mark - Data

- (void)setData
{
    NSArray *arr1 = @[
        @{  
            @"tag":@"0",
            @"title":@"字面值创建NSArray *array = @[someObject];//这种创建方式底层就是调用实例初始化方法"
        },
        @{ 
            @"tag":@"1",
            @"title":@"【- (instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects count:(NSUInteger)cnt;//实例初始化方法，返回nil"},
        @{ 
            @"tag":@"2",
            @"title":@"+ (instancetype)arrayWithObjects:(const ObjectType _Nonnull [_Nonnull])objects count:(NSUInteger)cnt;调用实例初始化方法"},
        @{ 
            @"tag":@"3",
            @"title":@"【+ (instancetype)arrayWithObject:(ObjectType)anObject;调用实例初始化方法"},
    ];
    
    NSArray *arr2 = @[
        @{ 
            @"tag":@"0",
            @"title":@"- (ObjectType)objectAtIndex:(NSUInteger)index;//返回nil;包含空数组，一个元素的数组 ，下标索引的方式访问 id value = array[3];"},
        @{
            @"tag":@"1",
            @"title":@"- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx;包含（1）：多元素数组下标索引的方式访问 id value = array[3];"},
        @{
            @"tag":@"2",
            @"title":@"- (NSArray<ObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes;//返回nil"},
        @{
            @"tag":@"3",
            @"title":@"【-(NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;//返回NSNotFound"},
        @{
            @"tag":@"4",
            @"title":@"【- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;//返回NSNotFound"},

        @{ 
            @"tag":@"5",
            @"title":@"【-(NSArray *)subarrayWithRange:(NSRange)range;//返回nil"},
        
        @{ 
            @"tag":@"6",
            @"title":@"【- (NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject;//返回self"},
    ];
    

    NSArray *arr3 = @[
        @{ 
            @"tag":@"0",
            @"title":@"- (ObjectType)objectAtIndex:(NSUInteger)index; //返回nil;"},
        @{
            @"tag":@"1",
            @"title":@"- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx; //返回nil 包含（1）：多元素数组下标索引的方式访问 id value = array[3];"},
        @{
            @"tag":@"2",
            @"title":@"增加某个对象:anObj=空的时候崩溃;不作处理//会调用insertObject:atIndex:方法 - (void)addObject:(ObjectType)anObject;"},
        @{
            @"tag":@"3",
            @"title":@"增加某个对象：//不作处理- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index;"},
        @{
            @"tag":@"4",
            @"title":@"【 增加某组对象：//不作处理- (void)insertObjects:(NSArray<ObjectType> *)objects atIndexes:(NSIndexSet *)indexes;"},
        @{
            @"tag":@"5",
            @"title":@" 删除某个对象：//不作处理- (void)removeObjectAtIndex:(NSUInteger)index;"},
        @{
            @"tag":@"6",
            @"title":@"【 删除某组范围的对象：//不作处理- (void)removeObjectsInRange:(NSRange)range;"},
        @{
            @"tag":@"7",
            @"title":@"【 删除某组索引的对象：//不作处理- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;"},
        
        @{ 
            @"tag":@"8",
            @"title":@"【删除所有哈希值匹配项//range越界不作处理- (void)removeObject:(ObjectType)anObject inRange:(NSRange)range;"},
        @{
            @"tag":@"9",
            @"title":@"【删除所有物理地址匹配项//range越界不作处理- (void)removeObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;"}
    ];
    
    NSArray *array = @[
        @{
            @"section":@"allAlloc",
            @"sectionTitle":@"NSArray初始化方法",
            @"list":arr1
        },
        @{
            @"section":@"NSArray",
            @"sectionTitle":@"NSArray实例方法",
            @"list":arr2
        },
        @{
            @"section":@"NSMutableArray",
            @"sectionTitle":@"NSMutableArray实例方法",
            @"list":arr3
        },
    ];
    self.dataArray = array;
}

#pragma mark - TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dic = [self.dataArray objectAtIndex:section];
    NSArray *list = [dic objectForKey:@"list"];
    return list.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.section];
    NSArray *list = [dic objectForKey:@"list"];
    NSDictionary *rowDic = [list objectAtIndex:indexPath.row];
    NSString *value = [rowDic objectForKey:@"title"];
    cell.textLabel.text =  value;
    cell.textLabel.numberOfLines = 0;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSDictionary *dic =  [self.dataArray objectAtIndex:section];
    NSString *title = [dic objectForKey:@"sectionTitle"];
    return title;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.section];
    NSString *section = [dic objectForKey:@"section"];
    NSArray *list = [dic objectForKey:@"list"];
    NSDictionary *rowDic = [list objectAtIndex:indexPath.row];
    NSString *tag = [rowDic objectForKey:@"tag"];

    if ([section isEqualToString:@"allAlloc"]) {
        if ([tag isEqualToString:@"0"]) {
            NSString *str = nil;
            NSArray *arr = @[str];
        }
        else{
            NSString *str = nil;
            NSArray *arr = [NSArray arrayWithObject:str];
        }
    }
    else if ([section isEqualToString:@"NSArray"]) {
      
        if ([tag isEqualToString:@"0"]) {
            NSArray *arr = @[@"1"];
            [arr objectAtIndex:2];
        }
        else if ([tag isEqualToString:@"1"]) {
            NSArray *arr = @[@"1"];
            id obj = arr[2];
        }
        else if ([tag isEqualToString:@"2"]) {
            NSArray *arr = @[@"1"];
            [arr objectsAtIndexes:[NSIndexSet indexSetWithIndex:2]];
        }
        else if ([tag isEqualToString:@"3"]) {
            NSArray *arr = @[@"1"];
            [arr indexOfObject:@"2" inRange:NSMakeRange(0, 2)];
        }
        else if ([tag isEqualToString:@"4"]) {
            NSArray *arr = @[@"1"];
            NSUInteger a = [arr indexOfObjectIdenticalTo:nil inRange:NSMakeRange(0, 2)];
            NSLog(@"%@",@(a));
        }
        else if ([tag isEqualToString:@"5"]) {
            NSArray *arr = @[@"1"];
            NSArray *arr1 = [arr subarrayWithRange:NSMakeRange(0, 2)];
            NSLog(@"%@",arr1);
        }
        else if ([tag isEqualToString:@"6"]) {
            NSArray *arr = @[@"1"];
            NSArray *arr1 = [arr arrayByAddingObject:nil];
            NSLog(@"%@",arr1);
        }
        else{
        }
    }
    else if ([section isEqualToString:@"NSMutableArray"]) {
        if ([tag isEqualToString:@"0"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            id obj = [arr objectAtIndex:3];
        }
        else if ([tag isEqualToString:@"1"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            id obj = arr[3];
        }
        else if ([tag isEqualToString:@"2"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            [arr addObject:nil];
        }
        else if ([tag isEqualToString:@"3"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            [arr insertObject:nil atIndex:5];
        }
        else if ([tag isEqualToString:@"4"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:3];
            [indexes addIndex:4];
            [arr insertObjects:@[@"1",@"3"] atIndexes:indexes];
        }
        else if ([tag isEqualToString:@"5"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            [arr removeObjectAtIndex:3];
        }
        else if ([tag isEqualToString:@"6"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            [arr removeObjectsInRange:NSMakeRange(0, 3)];
        }
        else if ([tag isEqualToString:@"7"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            [arr removeObjectsAtIndexes:[NSIndexSet indexSetWithIndex:3]];
        }
        else if ([tag isEqualToString:@"8"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            [arr removeObject:@"" inRange:NSMakeRange(0, 3)];
        }
        else if ([tag isEqualToString:@"9"]) {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
            [arr removeObjectIdenticalTo:@"1" inRange:NSMakeRange(0, 4)];
        }
    }
    else{

    }
    

}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
